
/****************************************************************************
 * Included Files
 ****************************************************************************/
#include "pca9555.h"
#include "sdp800.h"

static uint8_t pca9555_write(uint8_t i_cmd,uint8_t *data,uint8_t lenth)
{
	Error error;
	  // write a start condition
	if(lenth > 2)
		return ERROR_IVALID_PARAMETER;
	USR_IIC_Start();
	// write the sensor I2C address with the write flag
	error = USR_IIC_Send_Byte((PCA9555_ADDR << 1 | 0x00),1);
	  // write 8 bits of the command
	if(ERROR_NONE == error) {
		error = USR_IIC_Send_Byte(i_cmd,1);
	}
	// write 8 bits of the data to the chip
	if(ERROR_NONE == error) {
	    error = USR_IIC_Send_Byte(*data,1);
	}
	if(lenth == 2)
	{
		if(ERROR_NONE == error) {
			error = USR_IIC_Send_Byte(*(data+1),1);
		}
	}
	USR_IIC_Stop();
	return error;
}

static uint8_t pca9555_read(uint8_t i_cmd,uint8_t *data,uint8_t lenth)
{
	Error error;

	if(lenth > 2)
		return ERROR_IVALID_PARAMETER;
	// write a start condition
	USR_IIC_Start();
	// write I2C address with the write flag
	error = USR_IIC_Send_Byte((PCA9555_ADDR << 1 | 0x00),1);
	// write 8 bits of the command
	if(ERROR_NONE == error) {
		error = USR_IIC_Send_Byte(i_cmd,1);
	}
	// write I2C address with the read flag
	if(ERROR_NONE == error) {
		USR_IIC_Start();
		error = USR_IIC_Send_Byte((PCA9555_ADDR << 1 | 0x01),1);
	}
	if(ERROR_NONE == error){
		*data = USR_IIC_Read_Byte(1);
		if(lenth == 2)
			*(data+1) = USR_IIC_Read_Byte(0);
	}
	USR_IIC_Stop();
	return error;
}

/****************************************************************************
 * Name: pca9555_setbit
 *
 * Description:
 *  Write a bit in a register pair
 *
 ****************************************************************************/
uint8_t pca9555_setbit(uint8_t pin, uint8_t bitval)
{
	uint8_t buf[2];
	Error error;

	if (pin > 15)
    {
      return ERROR_IVALID_PARAMETER;
    }

	error = pca9555_read(PCA9555_REG_OUT0,buf,2);
	if(error == ERROR_NONE)
	{
		if(pin <= 7)
		{
			if (bitval)
			{
				buf[0] |= (1 << pin);
			}
			else
			{
				buf[0] &= ~(1 << pin);
			}
		}
		else if(pin > 7)
		{
			if (bitval)
			{
				buf[1] |= (1 << (pin-8));
			}
			else
			{
				buf[1] &= ~(1 << (pin-8));
			}
		}
		error = pca9555_write(PCA9555_REG_OUT0,buf,2);
	}
	return error;
}

#ifdef NOT_USED
uint8_t pca9555_getbit( uint8_t pin, uint8_t *val)
{
  uint8_t buf[2];
  Error error;

  if (pin > 15)
  {
      return -1;
  }
  error = pca9538_read(PCA9555_REG_OUT0,buf,2);
  if(pin <= 7)
	  *val = (buf[0] >> pin) & 1;
  else if(pin > 7)
	  *val = (buf[1] >> (pin-8)) & 1;
  return error;
}
#endif

uint8_t pca9555_getall(uint8_t *data)
{
	Error error;
	uint8_t buf[2];

	error = pca9555_read(PCA9555_REG_OUT0,buf,2);
	if(error == ERROR_NONE)
	{
		*data = buf[0];
		*(data+1) = buf[1];
	}
	return error;
}

uint8_t pca9555_init(void)
{
	Error error;
	uint8_t buf[2] = {PCA9555_DERECTION0,PCA9555_DERECTION1};

	error = pca9555_write(PCA9555_REG_CFG0,buf,2);//set all pin as output port
	return error;
}
